<?php
/**
 * Created by PhpStorm.
 * User: superxing-cto
 * Date: 17/3/8
 * Time: 10:33
 */

namespace App\Models;

use App\Http\Helper\UrlSchemaHelper;

/**
 * Class Banner
 * @property string cover 封面图
 * @property string type 链接类型
 * @property string web_url web链接
 * @property string source_type 资源类型，image/video
 * @property integer sort 排序
 * @property integer type_id 类型id
 * @property boolean status 状态
 * @property dateTime start_time 展示开始时间
 * @property dateTime end_time 展示结束时间
 * @property string method 展示方式：always:一直展示、only：只展示一次
 * @property integer topic_id 主题id
 */
class Banner extends ApplicationModel
{
    protected $fillable = [
        'cover',
        'type',
        'sort',
        'status',
        'web_url',
        'source_type',
        'type_id',
        'start_time',
        'end_time',
        'method',
        'topic_id',
        'share_title',
        'share_subtitle',
        'share_url',
        'share_content',
        'share_head',
        'video_id'


    ];

    public function scopeRelease($query)
    {
        return $query->where('status', true)->where(function ($query) {
            return $query->whereNull('start_time')->orWhere(function ($q) {
                return $q->where('start_time', '<=', date('Y-m-d H:i:s'))->where('end_time', '>=', date('Y-m-d H:i:s'));
            });
        });
    }

    public function scopeAtMain($query)
    {
        return $query->where('type', 'main');
    }

    public function scopeAtDynamic($query)
    {
        return $query->where('type', 'Dynamic');
    }

    public function scopeAtSkill($query)
    {
        return $query->where('type', 'Skill');
    }

    public function scopeAtMainLayer($query)
    {
        return $query->where('type', 'main_layer');
    }

    public function scopeAtSplash($query)
    {
        return $query->where('type', 'splash');
    }

    public function web_url()
    {
        if ($this->source_type == 'image' && $this->web_url) {
            return UrlSchemaHelper::WEB . $this->web_url;
        }
        return $this->web_url ?: '';
    }

    public function share_content()
    {
        $do_main = config('constants.DO_MAIN');
        $share_url = empty($this->web_url)? $do_main .'/app/download.html':$this->web_url;
        $arr = array(
            'qqShare' => array('title' => $this->share_title, 'content' => $this->share_subtitle, 'url' => $share_url, 'cover' => $this->cover),
            'wxShare' => array('title' => $this->share_title, 'content' =>$this->share_subtitle, 'url' => $share_url, 'cover' => $this->cover),
            'wxQuan' => array('title' => $this->share_title, 'content' =>$this->share_subtitle, 'url' => $share_url, 'cover' => $this->cover),
            'wbShare' => array('title' => $this->share_title, 'content' => $this->share_subtitle, 'url' => $share_url, 'cover' => $this->cover),
            'qqZone' => array('title' =>$this->share_title, 'content' => $this->share_subtitle, 'url' =>$share_url, 'cover' => $this->cover),
        );

        return $arr;
    }
}